function findLongestRepeatedSubstring(str) {
  const n = str.length;
  const suffixArr = new Array(n);
  for (let i = 0; i < n; i++) {
    suffixArr[i] = i;
  }
  suffixArr.sort((a, b) => {
    while (a < n && b < n) {
      if (str[a] !== str[b]) {
        return str[a].charCodeAt() - str[b].charCodeAt();
      }
      a++;
      b++;
    }
    return b - a;
  });
  const lcpArr = new Array(n);
  let k = 0;
  for (let i = 0; i < n; i++) {
    if (suffixArr[i] === 0) {
      lcpArr[suffixArr[i]] = 0;
      continue;
    }
    const j = suffixArr[i - 1];
    while (i + k < n && j + k < n && str[i + k] === str[j + k]) {
      k++;
    }
    lcpArr[suffixArr[i]] = k;
    k = Math.max(k - 1, 0);
  }
  let maxLen = 0;
  let maxIndex = 0;
  for (let i = 1; i < n; i++) {
    if (lcpArr[i] > maxLen) {
      maxLen = lcpArr[i];
      maxIndex = i;
    }
  }
  return str.substring(suffixArr[maxIndex], suffixArr[maxIndex] + maxLen);
}

console.log(findLongestRepeatedSubstring('banana')); 
	